package com.smart.service.impl;

import com.smart.common.utils.AssertUtil;
import com.smart.context.SessionContext;
import com.smart.mapper.JiansheUnitMapper;
import com.smart.mapper.StreetMapper;
import com.smart.mapper.UnitMapper;
import com.smart.service.CommonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author: zyh
 * @date: 2023-07-04
 */
@Service
public class CommonServiceImpl implements CommonService {
    @Autowired
    private UnitMapper unitMapper;
    @Autowired
    private JiansheUnitMapper jiansheUnitMapper;
    @Autowired
    private StreetMapper streetMapper;

    /**
     * 云监管-单位名称下拉列表查询
     * @param
     * @return
     */
    @Override
    public Object selectOperateUnitName(Map<String,Object> dataMap) {
        Object a = dataMap.get("unitType");
        Object s = dataMap.get("projectId");
        Long projectId = null;
        if (!ObjectUtils.isEmpty(s)){
            projectId = Long.valueOf(s.toString());
        }
        String unitType = null;
        if (!ObjectUtils.isEmpty(a)){
            if ("ALL".equals(a.toString())){
                unitType = null;
            }else {
                unitType = a.toString();
            }
        }else {
            AssertUtil.assertTrue(false,"unitType不能为空");
        }
        List<Map<String,Object>> unitNameList = unitMapper.selectAllUnitName(unitType,projectId);
        List<Map<String, Object>> unitName = unitNameList.stream().filter(unit -> !ObjectUtils.isEmpty(unit.get("unitName"))&&!ObjectUtils.isEmpty(unit.get("unitDetailName"))).collect(Collectors.toList());
        return unitName;
    }

    /**
     * 云服务-单位名称下拉列表查询
     * @param unitType
     * @return
     */
    @Override
    public Object selectServiceUnitName(String unitType) {
        if ("ALL".equals(unitType)){
            unitType = null;
        }
        Long projectId = SessionContext.getProjectId();
        List<Map<String,Object>> unitNameList = unitMapper.selectAllUnitName(unitType,projectId);
        List<Map<String, Object>> unitName = unitNameList.stream().filter(unit -> !ObjectUtils.isEmpty(unit.get("unitName"))&&!ObjectUtils.isEmpty(unit.get("unitDetailName"))).collect(Collectors.toList());
        return unitName;
    }

    /**
     * 云管理-单位名称下拉列表查询
     * @param unitType
     * @return
     */
    @Override
    public Object selectManageUnitName(String unitType) {
        if ("ALL".equals(unitType)){
            unitType = null;
        }
        Long jiansheUnitId = SessionContext.getJiansheUnitId();
        List<Map<String,Object>> unitNameList = unitMapper.selectAllUnitNameByJsId(unitType,jiansheUnitId);
        List<Map<String, Object>> unitName = unitNameList.stream().filter(unit -> !ObjectUtils.isEmpty(unit.get("unitName"))&&!ObjectUtils.isEmpty(unit.get("unitDetailName"))).collect(Collectors.toList());
        return unitName;
    }

    /**
     * 云管理项目下拉列表查询
     * @return
     */
    @Override
    public Object selectProjectList() {
        Long jiansheUnitId = SessionContext.getJiansheUnitId();
        List<Map<String,Object>> mapList = jiansheUnitMapper.selectAllProject(jiansheUnitId);
        return mapList;
    }

    @Override
    public Object queryAllProvince() {
        List<Map<String,Object>> list = streetMapper.queryAllProvince();
        return list;
    }

    @Override
    public Object queryCityByCode(String code) {
        List<Map<String,Object>> list = streetMapper.queryCityByCode(code);
        return list;
    }

    @Override
    public Object queryAreaByCode(String code) {
        List<Map<String,Object>> list = streetMapper.queryAreaByCode(code);
        return list;
    }
}
